<?php
namespace app\controller\garden;

use think\facade\Db;
use app\BaseController;

class Index extends BaseController{

    private static String $total;

    public function initialize(){
       
    }

    /**
     * 获取花种类
     */
    public function getFlowerTypes(){
        $types = Db::name('flower_type')->select();
        return BaseController::returnSuccessMsg('10001',$types);
    }

    /**
     * 判断是否为最后一页
     */
    public static function isEnd(){
         $param = input("get.");
         Self::$total = Db::name("garden")
         ->alias('g')
         ->join('garden_type gt','gt.garden_id=g.id')
         ->join('flower_type ft','ft.id=gt.type_id')
         ->where('ft.id',$param['type'])
         ->count();
         if ($param['start'] > ceil(Self::$total/$param['num']) ) {
             print_r("已达到最大页");
             die();
         }
    }

    /**
     * ！！需要改善，减少不必要内容的查询！！
     * 花屋分页查询
     */
    public function getFlowerByPage(){
        Self::isEnd();
        // 获取请求里面所有数据
        $param = input("get.");
        // 每次请求开始的页面是 页数-1再乘以每页数量
        $start = ($param['start'] - 1)*$param['num'];
        // 查出分页数据
        $list = Db::name("garden")
        ->alias('g')
        ->join('garden_type gt','gt.garden_id=g.id')
        ->join('flower_type ft','ft.id=gt.type_id')
        ->where('ft.id',$param['type'])
        ->limit($start,$param['num'])
        ->field('g.*,ft.name as type')
        ->select()
        ->toArray();
        foreach($list as $key=>$value){
           $list[$key]['img'] = explode(",,",$value['img']);
        }
        // print_r($list);
        $data = ["list"=>$list,"total"=>Self::$total];
        return BaseController::returnSuccessMsg('10001',$data);
    }


    /**
     * 花的详情页
     * 一个花对多个花类型
     * 一个花对多个套餐
     */
    public function getFlowerById(){
        $fid = input('get.fid');
        $uid = input("get.uid");
        // 查询该花的具体信息
        $flower = Db::name('garden')->where('id',$fid)->find();
        // 用户是否收藏
        $hasCollect = Db::name('garden')
        ->alias('g')
        ->join('user_collects uc','uc.garden_id=g.id')
        ->where(['uc.user_id'=>$uid,'g.id'=>$fid])
        ->count();
        //花类型查询
        $type = Db::name('garden')
        ->alias('g')
        ->join('garden_type gt','gt.garden_id=g.id')
        ->join('flower_type ft','ft.id=gt.type_id')
        ->where('g.id',$fid)
        ->field('ft.name as type')
        ->select();
        //花套餐查询
        $meals = Db::name('garden')
        ->alias('g')
        ->join('garden_meal gm','gm.garden_id=g.id')
        ->join('meals m','m.id=gm.meal_id')
        ->where('g.id',$fid)
        ->field('m.name as meal,m.id as mealId')
        ->select();
        // 赋值新属性
        $flower['img'] = explode(",,",$flower['img']);
        $flower['type'] = $type;
        $flower['meal'] = $meals;
        if ($hasCollect > 0 ) {
            $flower['hasCollect'] = true;
        }else{
            $flower['hasCollect'] = false;
        }
        
        return BaseController::returnSuccessMsg('10001',$flower);
    }



}

?>